/**
 * Filename: YNTimeLineActivity.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/5/16 23:40
 */
package com.github.postsanf.yinian.activity;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.util.SparseArray;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.github.postsanf.yinian.BaseFragmentActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.guide.FragmentAdapter;
import com.github.postsanf.yinian.timeline.BaseTimelineFragment;
import com.github.postsanf.yinian.timeline.FirstFragment;
import com.github.postsanf.yinian.timeline.SecondFragment;
import com.github.postsanf.yinian.timeline.ThirdFragment;
import com.github.postsanf.yinian.view.NewsViewPager;
import com.umeng.analytics.MobclickAgent;

/**
 * 时光罗盘
 */
public class YNTimeLineActivity extends BaseFragmentActivity {
    final float PARALLAX_COEFFICIENT = 1.2f;
    final float DISTANCE_COEFFICIENT = 0.5f;
    private String mCurYear = "0";
    private String mCurMonth = "0";
    private String mCurGroup = "0";
    NewsViewPager mPager;
    FragmentAdapter mAdapter;
    SparseArray<int[]> mLayoutViewIdsMap = new SparseArray<>();
    private int[] mAnimationViewIds = {R.id.iv_timeline_start1, R.id.iv_timeline_start2};
    private Handler handler = new Handler();
    private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            doBGAnimation();
        }
    };

    public void setCurYear(String mCurYear) {
        this.mCurYear = mCurYear;
    }

    public void setCurMonth(String mCurMonth) {
        this.mCurMonth = mCurMonth;
    }

    public void setCurGroup(String mCurGroup) {
        this.mCurGroup = mCurGroup;
    }

    public String getCurGroup() {
        return mCurGroup;
    }

    public String getCurYear() {
        return mCurYear;
    }

    public String getCurMonth() {
        return mCurMonth;
    }

    private void addGuide(BaseTimelineFragment fragment) {
        mAdapter.addItem(fragment);
        mLayoutViewIdsMap.put(fragment.getRootViewId(), fragment.getChildViewIds());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.timeline);
        mPager = (NewsViewPager) findViewById(R.id.pager);
        mAdapter = new FragmentAdapter(getSupportFragmentManager());
        addGuide(new FirstFragment());
        addGuide(new SecondFragment());
        addGuide(new ThirdFragment());
        mPager.setAdapter(mAdapter);
        mPager.setPageTransformer(true, new ParallaxTransformer(PARALLAX_COEFFICIENT, DISTANCE_COEFFICIENT));
        handler.postDelayed(mRunnable, 1000);
    }

    // 开始背景动画
    private void doBGAnimation() {
        findViewById(mAnimationViewIds[0]).startAnimation(getBGAnimation(R.anim.anim_stars_bg1));
        findViewById(mAnimationViewIds[1]).startAnimation(getBGAnimation(R.anim.anim_stars_bg2));
    }

    // 根据动画ID获取Animation
    private Animation getBGAnimation(int resID) {
        Animation animation = AnimationUtils.loadAnimation(this, resID);
        return animation;
    }

    public void switchPage(int pageNum) {
        mPager.setCurrentItem(pageNum);
        if (pageNum == 2) {
            ((ThirdFragment) mAdapter.getItem(2)).doChange();
        }
    }

    class ParallaxTransformer implements ViewPager.PageTransformer {

        float parallaxCoefficient;
        float distanceCoefficient;

        public ParallaxTransformer(float parallaxCoefficient, float distanceCoefficient) {
            this.parallaxCoefficient = parallaxCoefficient;
            this.distanceCoefficient = distanceCoefficient;
        }

        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        @Override
        public void transformPage(View page, float position) {
//			float scrollXOffset = page.getWidth() * parallaxCoefficient;
//
//			ViewGroup pageViewWrapper = (ViewGroup) page;
//			@SuppressWarnings("SuspiciousMethodCalls")
//			int[] layer = mLayoutViewIdsMap.get(pageViewWrapper.getId());
//
//			for (int id : layer) {
//				View view = page.findViewById(id);
//				if (view != null) {
//					view.setTranslationX(scrollXOffset * position);
//				}
//				scrollXOffset *= distanceCoefficient;
//			}
        }
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
